#!/usr/bin/python

import gtk
import os
import sys
import utils

class MainWindow(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self)
        
        self.set_size_request(300, 300)
        self.set_title("Hydroxygen Configurator")
        icon=utils.getPixbufFromName("/opt/Hydroxygen/images/logo.png", 48)
        self.set_icon_list(icon)
        self.set_border_width(5)
        themeicon=utils.getPixbufFromName("/opt/Hydroxygen/images/themelogo.png", 48)
        
        VBox=gtk.VBox(spacing=5)
        self.add(VBox)
        
        Icons=gtk.HBox(spacing=5)
        if themeicon!=None:
            image=gtk.Image()
            image.set_from_pixbuf(themeicon)
            Icons.pack_start(image, True, True, 0)

        Label=gtk.HBox(spacing=5)
        Label.pack_start(gtk.Label("Configure Hydroxygen Iconset."), True, True, 0)
        
        Actions=gtk.HBox()
        
        VButtonBox=gtk.VButtonBox()
        VButtonBox.set_layout(gtk.BUTTONBOX_START)
        VButtonBox.set_spacing(5)
        
        upImage=gtk.Image() 
        upImage.set_from_pixbuf(utils.getPixbufFromName("gtk-refresh",24))
        
        updateButtonTgz=gtk.Button("Update From .tar.gz")
        updateButtonTgz.set_image(upImage)
        updateButtonTgz.connect("clicked", self.updateTGZ)

        #updateButtonSvn=gtk.Button("Update From Svn")
        #updateButtonSvn.set_image(upImage)
        #updateButtonSvn.connect("clicked", self.updateSVN)
        
        configureButton=gtk.Button("Configure")
        confImage=gtk.Image()
        confImage.set_from_pixbuf(utils.getPixbufFromName("gtk-preferences",24))
        configureButton.set_image(confImage)
        configureButton.connect("clicked", self.configure)
        
        restoreButton=gtk.Button("Restore")
        restoreButton.connect("clicked", self.restore)
        restImage=gtk.Image()
        restImage.set_from_pixbuf(utils.getPixbufFromName("back",24))
        restoreButton.set_image(restImage)
        
        if os.path.exists(os.path.expanduser("~")+"/.icons/hydroxygen/") and os.path.isfile(os.path.expanduser("~")+"/.icons/hydroxygen/options.csv"):
            VButtonBox.add(updateButtonTgz)
            #VButtonBox.add(updateButtonSvn)
            VButtonBox.add(configureButton)
            if os.path.isfile(os.path.expanduser("~")+"/.hydroxygenrc"):
                VButtonBox.add(restoreButton)
        else:
            updateButtonTgz.set_label("Install Hydroxygen from .tar.gz")
            #updateButtonSvn.set_label("Install Hydroxygen from svn")
            VButtonBox.add(updateButtonTgz)
            #VButtonBox.add(updateButtonSvn)
        
        Actions.pack_start(VButtonBox)
        
        ExitBox=gtk.HButtonBox()

        infoButton=gtk.Button(gtk.STOCK_ABOUT)
        infoButton.set_use_stock(True)
        infoButton.connect("clicked", self.info)

        exitButton=gtk.Button(gtk.STOCK_CLOSE)
        exitButton.set_use_stock(True)
        exitButton.connect("clicked", self.exit)

        ExitBox.add(infoButton)
        ExitBox.add(exitButton)
        ExitBox.set_layout(gtk.BUTTONBOX_SPREAD)
        ExitBox.set_spacing(5)
        
        
        VBox.pack_start(Icons, False)
        VBox.pack_start(Label, False)
        VBox.pack_start(Actions)
        VBox.pack_end(ExitBox, False)
                
        self.connect("delete-event", self.exit)
        self.set_resizable(False)
        self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
        self.show_all()
        gtk.main()
    
    def updateTGZ(self, obj=None):
        print "update"
        import UpdateTGZ
        self.hide()
        UpdateTGZ.UpdateTGZ()
        if os.path.isfile(os.path.expanduser("~")+"/.icons/hydroxygen/options.csv")==False:
            os.system("cp /opt/Hydroxygen/options.csv " + os.path.expanduser("~")+"/.icons/hydroxygen/options.csv")
        self.restore()
        self.show()

    def updateSVN(self, obj=None):
        print "update"
        import UpdateSVN
        self.hide()
        UpdateSVN.UpdateSVN()
        if os.path.isfile(os.path.expanduser("~")+"/.icons/hydroxygen/options.csv")==False:
            os.system("cp /opt/Hydroxygen/options.csv " + os.path.expanduser("~")+"/.icons/hydroxygen/options.csv")
        self.restore()
        self.show()
        
        
    def restore(self, obj=None):
        print "restore"
        list=utils.read_last_options()
        if list is not None:
            for el in list:
                pid=os.fork()
                if pid==0:
                    os.chdir(os.path.expanduser("~")+"/.icons/hydroxygen/")
                    os.execvp("bash", ["bash", "change-type.sh", el.split("/")[0]])
            utils.Refresh()
            print "restored"
        else:
            print "nessuna conf"
    
    def configure(self, obj=None):
        print "configure"
        import SetupConfiguration
        self.hide()
        SetupConfiguration.Setup()
        self.show()
    
    def info(self, obj=None):
        print "show info"
        import Info
        Info.Info()
    
    def exit(self, obj, event=None):
        self.hide()
        gtk.main_quit()
        sys.exit(0)

MainWindow()
